package heven.holt.wan.android.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import heven.holt.wan.android.databinding.ItemCoinRankBinding
import heven.holt.wan.android.model.UserCoinInfo

class CoinRankAdapter :
    PagingDataAdapter<UserCoinInfo, CoinRankAdapter.CoinRankViewHolder>(CoinRankComparator()) {

    override fun onBindViewHolder(holder: CoinRankViewHolder, position: Int) {
        holder.binding.userCoinInfo = getItem(position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CoinRankViewHolder =
        CoinRankViewHolder(
            ItemCoinRankBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )

    class CoinRankViewHolder(
        val binding: ItemCoinRankBinding
    ) : RecyclerView.ViewHolder(binding.root)

    class CoinRankComparator : DiffUtil.ItemCallback<UserCoinInfo>() {
        override fun areItemsTheSame(oldItem: UserCoinInfo, newItem: UserCoinInfo): Boolean =
            oldItem.userId == newItem.userId

        override fun areContentsTheSame(oldItem: UserCoinInfo, newItem: UserCoinInfo): Boolean =
            oldItem == newItem
    }
}